package com.combetter.changxing.viewmodel;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.combetter.changxing.MyApp;
import com.combetter.changxing.db.AppDatabase;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.ui.activity.ForgetPwdActivity;
import com.combetter.changxing.ui.activity.MainActivity;
import com.combetter.changxing.ui.activity.RegisterActivity;
import com.combetter.changxing.ui.dialog.LoadingDialog;
import com.combetter.changxing.util.AppManager;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.ToastUtils;
import com.combetter.changxing.viewmodel.base.BaseViewModel;


import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/6.
 */
public class LoginViewModel extends BaseViewModel {
    private AppDatabase appDatabase;

    //手机号码
    public ObservableField<String> userName = new ObservableField<>("");
    //密码
    public ObservableField<String> password = new ObservableField<>("");

    //第三方登录昵称
    public ObservableField<String> nickname = new ObservableField<>("");
    //第三方登录头像
    public ObservableField<String> figureurl = new ObservableField<>("");


    public LoginViewModel(@NonNull Application application) {
        super(application);
        appDatabase = MyApp.getInstance().getDatabase();
    }

    /**
     * 登录
     *
     * @param view
     */
    public void login(View view) {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.error("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.error("请输入密码！");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", userName.get());
        map.put("password", password.get());
        login(view.getContext(), map);
    }

    public void login(Context context, HashMap<String, String> map) {
        LoadingDialog dialog = LoadingDialog.getInstance(context);
        dialog.show();
//        mHttpRequest.createService(APIService.class)
//                .login(map)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeOn(Schedulers.io())
//                .doOnError(throwable -> dialog.dismiss())
//                .subscribe(userHttpResult -> {
//                    dialog.dismiss();
//                    LogUtil.e("登录返回：", userHttpResult);
//                    if (userHttpResult.getCode() == 200) {
//                        User user = userHttpResult.getData();
//                        JPushInterface.setAlias(context, 0, user.getUid());
//                        user.setPhone(user.getPhone());
//                        appDatabase.userDao().insertUser(user);
//                        context.startActivity(new Intent(context, MainActivity.class));
//                        AppManager.finishAllActivity();
//                    } else if (userHttpResult.getCode() == 610) {  //第三方新用户
//                        Intent intent = new Intent(context, RegisterActivity.class);
//                        intent.putExtra("openId", map.get("openId"));
//                        intent.putExtra("nickname", nickname.get());
//                        intent.putExtra("figureurl", figureurl.get());
//                        context.startActivity(intent);
//                    } else {
//                        ToastUtils.error(userHttpResult.getMsg());
//                    }
//                });
    }


    /**
     * 注册
     *
     * @param view
     */
    public void register(View view) {
//        view.getContext().startActivity(new Intent(view.getContext(), RegisterActivity.class));
    }

    /**
     * 忘记密码
     *
     * @param view
     */
    public void forgetPwd(View view) {
//        view.getContext().startActivity(new Intent(view.getContext(), ForgetPwdActivity.class));
    }

    public void codeLogin(View view) {

    }
    /**
     * QQ第三方登录
     */
//    public void qqLogin(Activity activity, IUiListener listener) {
//        String SCOPE = "all";
//        mTencent = Tencent.createInstance(QQ_APPID, activity);
//        if (!mTencent.isSessionValid()) {
//            mTencent.login(activity, SCOPE, listener);
//        }
//    }

    /**
     * 微信第三方登录
     */
//    public void wechatLogin() {
//        SendAuth.Req req = new SendAuth.Req();
//        req.scope = "snsapi_userinfo";
//        req.state = "wechat_sdk_demo_test";
//        api.sendReq(req);
//    }

//    public void regToWx(Activity activity) {
//        api = WXAPIFactory.createWXAPI(activity, WECHAT_APPID, true);
//        api.registerApp(WECHAT_APPID);
//    }

}
